from PySide6.QtUiTools import QUiLoader
from PySide6.QtWidgets import QHeaderView, QAbstractItemView, QTableWidgetItem, QMessageBox

from opsql import rootOp


class MCom():
    def __init__(self):
        self.ui = QUiLoader().load('view/电脑管理.ui')
        self.inittable()
        self.initcombo()
        self.ui.searchBtn.clicked.connect(self.inittable)
        self.ui.addBtn.clicked.connect(self.add)
        self.ui.modifyBtn.clicked.connect(self.modify)
        self.ui.deleteBtn.clicked.connect(self.delete)
        self.ui.tableWidget.cellClicked.connect(self.tableclicked)

    def tableclicked(self,row):
        self.ui.idShow.setText(self.ui.tableWidget.item(row, 0).text())
        if self.ui.tableWidget.item(row, 1).text() == '是':
            isfree = 1
        else:
            isfree = 0
        self.ui.isfreeShow.setCurrentIndex(self.ui.isfreeShow.findData(isfree))
        if self.ui.tableWidget.item(row, 2).text() == '是':
            damaged = 1
        else:
            damaged = 0
        self.ui.damagedShow.setCurrentIndex(self.ui.damagedShow.findData(damaged))
        self.ui.ctnameShow.setText(self.ui.tableWidget.item(row, 3).text())
        self.ui.rnameShow.setText(self.ui.tableWidget.item(row, 4).text())

    def inittable(self):
        id = self.ui.idIn.text()
        rname = self.ui.rnameIn.text()
        ctname = self.ui.ctname.text()
        damaged = self.ui.damagedIn.currentData()
        isfree = self.ui.isfreeIn.currentData()
        result = rootOp.searchcomputer(id, rname, ctname, damaged, isfree)
        row = 0
        if result:
            row = len(result)
        self.ui.tableWidget.setRowCount(row)
        self.ui.tableWidget.setColumnCount(5)
        self.ui.tableWidget.setHorizontalHeaderLabels(
            ['电脑id', '是否空闲', '是否损坏', '电脑类别', '机房'])
        self.ui.tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
        self.ui.tableWidget.setEditTriggers(QAbstractItemView.EditTrigger.NoEditTriggers)
        self.ui.tableWidget.verticalHeader().setVisible(False)
        self.ui.tableWidget.setSelectionBehavior(QAbstractItemView.SelectionBehavior.SelectRows)

        for i in range(row):
            for j in range(5):
                data = QTableWidgetItem(str(result[i][j]))
                self.ui.tableWidget.setItem(i, j, data)

    def initcombo(self):
        self.ui.isfreeIn.addItem("请选择", -1)
        self.ui.isfreeIn.addItem("空闲", 1)
        self.ui.isfreeIn.addItem("占用", 0)
        self.ui.isfreeShow.addItem("请选择", -1)
        self.ui.isfreeShow.addItem("空闲", 1)
        self.ui.isfreeShow.addItem("占用", 0)
        self.ui.damagedIn.addItem("请选择", -1)
        self.ui.damagedIn.addItem("损坏", 1)
        self.ui.damagedIn.addItem("正常", 0)
        self.ui.damagedShow.addItem("请选择", -1)
        self.ui.damagedShow.addItem("损坏", 1)
        self.ui.damagedShow.addItem("正常", 0)

    def add(self):
        id = self.ui.idShow.text()
        isfree = self.ui.isfreeShow.currentData()
        damaged = self.ui.damagedShow.currentData()
        ctname = self.ui.ctnameShow.text()
        rname = self.ui.rnameShow.text()
        result = rootOp.addcomputer(id, isfree, damaged, ctname, rname)
        if result:
            QMessageBox.information(self.ui, '提示', '添加成功')
            self.inittable()
        else:
            QMessageBox.warning(self.ui, '提示', '添加失败')

    def modify(self):
        row = self.ui.tableWidget.currentRow()
        if row == -1:
            QMessageBox.warning(self.ui, '提示', '请选择要修改的行')
            return
        id = int(self.ui.tableWidget.item(row, 0).text())
        isfree = self.ui.isfreeShow.currentData()
        damaged = self.ui.damagedShow.currentData()
        ctname = self.ui.ctnameShow.text()
        rname = self.ui.rnameShow.text()
        result = rootOp.modifycomputer(id, isfree, damaged, ctname, rname)
        if result:
            QMessageBox.information(self.ui, '提示', '修改成功')
            self.inittable()
        else:
            QMessageBox.warning(self.ui, '提示', '修改失败')

    def delete(self):
        row = self.ui.tableWidget.currentRow()
        if row == -1:
            QMessageBox.warning(self.ui, '提示', '请选择要删除的行')
            return
        id = int(self.ui.tableWidget.item(row, 0).text())
        result = rootOp.deletecomputer(id)
        if result:
            QMessageBox.information(self.ui, '提示', '删除成功')
            self.inittable()
        else:
            QMessageBox.warning(self.ui, '提示', '删除失败')